﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.SqlConnector;
using ApplicationLibrary.ExtendedControls;
using Vintner.Database;
using Vintner.Library;

namespace Vintner.Forms
{
    public partial class SaleInvoice : ApplicationLibrary.DBManager.DocumentForm
    {
        public SaleInvoice()
        {
            InitializeComponent();
        }

        public SaleInvoice(GlobalInfo _gInfo)
            : base(_gInfo, "SaleInvoice")
        {
            InitializeComponent();
            fiscalNoControl1.globalInfo = _gInfo;
        }

        public override string DefineBrowseQuery()
        {
            QueryBuilder qb = new QueryBuilder();

            qb.Clear();
            qb.AddSelectAll(VI_SaleInvoice.Name);
            qb.AddFrom(VI_SaleInvoice.Name);

            return qb.Query;
        }

        public override string GetCodeColumn()
        {
            return VI_SaleInvoice.InvoiceNo.Name;
        }

        public override Table GetRadarTable()
        {
            return new VI_SaleInvoice();
        }



        protected override void OnAttachData()
        {
            dbManager = new dbmanagerSaleInvoice("saleinvoiceForm", base.providerType, ConnectionString);
            dbManager.AddMaster(this, VI_SaleInvoice.Name, true);

            rcbPayment.AddTableReader(dbManager.DBConnection, "Payment", AM_Descriptions.Description);

            fiscalNoControl1.AttachCounterType( GlobalInfo.GetCounter(global::Vintner.Properties.Resources.CN_SalesInvoice),
                                                GlobalInfo.CurrentDate, dbManager.DBConnection);
        }

        protected override void OnBindData()
        {
            BindControl(fiscalNoControl1, VI_SaleInvoice.InvoiceNo, "Counter");
            BindControl(calendarTextBox1, VI_SaleInvoice.Date);
            BindControl(rcbPayment, VI_SaleInvoice.Payment, "Text");

            BindCounter(ref fiscalNoControl1);
        }

        protected override void OnDisableControlsForNew()
        {
            //fiscalNoControl1.GetNewValue();
        }

        protected override void OnDisableControlsForEdit()
        {
            fiscalNoControl1.Enabled = false;
        }

        protected override bool OnBeforeSave()
        {
            fiscalNoControl1.GetNewValue();
            return true;
        }
    }

    #region DbManager dbmanagerSaleInvoice
    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerSaleInvoice : DBManager
    {
        public dbmanagerSaleInvoice(string name, ProviderType providerType, string conString)
            : base(name, providerType, conString)
        {
        }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override void dAdapter_RowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            //if (e.StatementType == StatementType.Insert)
            //{
            //    e.Row[VI_ItemsPrice.Item.Name] = GetColumn(VI_Items.Item, Pos);
            //}
            base.dAdapter_RowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new VI_SaleInvoice();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(this.Table.ForeignKey, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter(providerType, "@p1", SqlDbType.NVarChar, 21);
            sParam.Value = "";
            PList.Add(sParam);
            return PList;
        }

        protected override string CreateSlaveQuery(string name)
        {
            //if (name == VI_ItemsPrice.Name)
            //{
            //    VI_ItemsPrice ItemsPrice = new VI_ItemsPrice();
            //    QueryBuilder qB = new QueryBuilder();
            //    qB.AddSelect();
            //    qB.AddFrom(VI_ItemsPrice.Name);
            //    qB.AddCompare(VI_ItemsPrice.Item, "@p2");
            //    qB.AddOrder(VI_ItemsPrice.Category);
            //    return qB.Query;
            //}

            return "";
        }

        protected override List<SqlABParameter> CreateSlaveParam(string name)
        {
            //if (name == VI_ItemsPrice.Name)
            //{
            //    SqlABParameter sParam = new SqlABParameter(providerType, "@p2", SqlDbType.NVarChar, 21);
            //    sParam.Value = "";
            //    return sParam;
            //}
            return null;
        }
    }
    #endregion
}
